//
//  BGNetworkTools.swift
//  Weibo-walker
//
//  Created by 郭彬 on 16/3/6.
//  Copyright © 2016年 guobin. All rights reserved.
//

import UIKit
import AFNetworking

enum BGHTTPMethod :String {
    case GET = "GET"
    case POST = "POST"
}


class BGNetworkTools: AFHTTPSessionManager {
    

    
    //MARK: - 单例
    static let sharedTools:BGNetworkTools = {
       
        let tools = BGNetworkTools()
        
        return tools
        
    }()
    
    //MARK: - 封装网路下载工具类
    /// 发送请求
    ///
    /// - parameter urlString:  <#urlString description#>
    /// - parameter parameters: <#parameters description#>
    /// - parameter finished:   <#finished description#>
    
    func request(method:BGHTTPMethod = .GET,urlString: String,parameters:AnyObject?,finished:(response:AnyObject?,error:NSError?)->()){
        
        //定义，如果成功
        
        // 定义请求成功的闭包
        let success = {(dataTask: NSURLSessionDataTask, response: AnyObject?) -> Void in
            finished(response: response, error: nil)
        }
        
    
        // 定义请求失败的闭包
        let failure = {(dataTask: NSURLSessionDataTask?, error: NSError) -> Void in
            finished(response: nil, error: error)
        }
        if method == .GET {
            
            self.GET(urlString, parameters: parameters, success: success, failure: failure)
            
        }
        else{
            self.POST(urlString, parameters: parameters, success: success, failure: failure)
        
        }
        
    }

}
extension BGNetworkTools {
//    func load
}
